/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parsers;

import utils.ResultSetWrap;
import common.Application;
import common.Initializer;
import dao.Property;
import database.DatabaseObject;
import database.DatabaseObjectFactory;
import java.util.Collection;
import java.util.LinkedList;
import runtime.RuntimeInstanceFactory;
import templates.PropertyTemplate;
import templates.Template;

/**
 *
 * @author KAV
 */
public class ResultSetWrapParser implements Initializer {

    private ResultSetWrap rws;

    public ResultSetWrapParser(ResultSetWrap rws) {
        this.rws = rws;
    }

    @Override
    public Object init(Object obj) {
        short uid = (Short) obj;
        Collection<Property> properties = new LinkedList<Property>();
        Application app = Application.getInstance();
        Template template = app.getTemplate(uid);
        Iterable<PropertyTemplate> propertyTemplates = template.getPropertyTemplateCollection();
        RuntimeInstanceFactory factory = new DatabaseObjectFactory();
        for (PropertyTemplate pt : propertyTemplates) {
            DatabaseObject value = (DatabaseObject) factory.getObject(pt.getType());
            if (rws.getValue(pt.getName()) != null) {
                value.setValue(rws.getValue(pt.getName()));
            } else {
                if (pt.isNullEnabled()) {
                    value.setValue(pt.getNullValue());
                }
            }
            properties.add(new Property(pt.getUid(), value));
        }
        return properties;
    }

    @Override
    public void setManagedObject(Initializer.ObjectType obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
